package com.workManager.controller;

import com.github.pagehelper.PageInfo;
import com.wordnik.swagger.annotations.ApiOperation;
import com.workManager.base.BaseController;

import com.workManager.config.RespCode;
import com.workManager.service.DirectExpensesService;
import com.workManager.vo.DirectExpensesVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Thinkpad on 2017/3/7 0007.
 */
@Controller
@RequestMapping("/directExpenses")
public class DirectExpensesController extends BaseController{

    @Autowired
    private DirectExpensesService directExpensesService;

    @ApiOperation(value="项目直接费用查询",notes = "项目直接费用查询")
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public Object query(HttpServletRequest request,
                        @RequestParam(value = "pageNumber",required = false)String pageNumber,
                        @RequestParam(value="pageSize", required = false) String pageSize,
                        @RequestParam(value = "startTime", required =  false) String startTime,
                        @RequestParam(value = "endTime", required =  false) String endTime,
                        @RequestParam(value="projectCode", required = false) String projectCode,
                        @RequestParam(value = "projectName", required = false) String projectName,
                        @RequestParam(value = "loginCode", required = false) String loginCode){
        ModelMap modelMap = new ModelMap();

        Integer pagenum = pageNumber==null?1:Integer.parseInt(pageNumber);
        Integer pagesize = pageSize==null?10:Integer.parseInt(pageSize);

        Map<String,Object> queryParam = new HashMap<String, Object>();
        queryParam.put("pagenum",pagenum);
        queryParam.put("pagesize",pagesize);
        queryParam.put("startTime",startTime);
        queryParam.put("endTime",endTime);
        queryParam.put("projectCode",projectCode);
        queryParam.put("projectName",projectName);
        queryParam.put("loginCode",loginCode);

        PageInfo<DirectExpensesVo> tbDirectExpensesPageInfo = directExpensesService.query(queryParam);

        Map<String,Object> dataMap = new HashMap<String, Object>();
        dataMap.put("total",tbDirectExpensesPageInfo.getTotal());
        dataMap.put("rows",tbDirectExpensesPageInfo.getList());
        return  setSuccessModelMap(modelMap,dataMap);
    }

    @ApiOperation(value="直接费用Excel文件上传接口",notes="直接费用Excel文件上传接口")
    @RequestMapping(value="/upLoadExcel",method = RequestMethod.POST)
    public Object upLoadExcel(@RequestParam()MultipartFile file){

        if(file==null){
            return  null;
        }

        ModelMap modelMap = new ModelMap();
        String name = file.getOriginalFilename();
        long size = file.getSize();

        if(name==null||"".equals(name)&&size==0){
            return  null;
        }

        String msg = "上传成功";
        try {
            directExpensesService.uploadExcel(name,file);
        } catch (Exception e) {
            e.printStackTrace();
            msg = e.getMessage();
            return setModelMap(modelMap,RespCode.INTERNAL_SERVER_ERROR,msg);
        }
        return  setModelMap(modelMap, RespCode.OK,msg);

    }

}
